package com.gzz.orchid.context.domain.rbac.impl;

import com.gzz.orchid.common.core.util.I18nUtil;
import com.gzz.orchid.context.domain.rbac.IOrganizationRepository;
import com.gzz.orchid.context.domain.rbac.IOrganizationService;
import com.gzz.orchid.context.domain.rbac.entity.OrganizationDo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Objects;

/**
 * @author higmd
 */
@Slf4j
@Repository
@RequiredArgsConstructor
public class OrganizationService implements IOrganizationService {
    private final IOrganizationRepository repository;

    @Override
    public void checkCodeUnique(OrganizationDo organizationDo) throws IllegalArgumentException {
        OrganizationDo exsitsDo = repository.findByCode(organizationDo.getCode());
        if (Objects.nonNull(exsitsDo) && !exsitsDo.getId().equals(organizationDo.getId())) {
            throw new IllegalArgumentException(I18nUtil.ofMessage("组织编码[%s]已存在", organizationDo.getCode()));
        }
    }

    @Override
    public void checkCodeExists(String code) throws IllegalArgumentException {
        OrganizationDo exsitsDo = repository.findByCode(code);
        if (Objects.nonNull(exsitsDo)) {
            throw new IllegalArgumentException(I18nUtil.ofMessage("组织编码[%s]不存在", code));
        }
    }
}
